package start;

import common.AnalysisClient;
import common.AnalysisWare;
import common.Constants;
import common.DataAnalysis;
import org.yaml.snakeyaml.Yaml;
import util.AuthorConf;
import util.remote.RemoteConf;
import util.remote.RemoteConnect;
import util.remote.RemoteConnectResult;
import util.remote.RemoteType;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * 查询数据的入口
 * 需要配置两个文件
 * analysis.txt          待分析的活动数据code 逗号分割,一行
 * remotes.txt           待查询的平台 逗号分割一行
 * <p>
 * <p>
 * <p>
 * Created by J.Chen on 2017/5/19.
 */
public class StartApp {

    private static List<RemoteConf> remotes;

    /**
     * debug 配置
     */
    private static void initRemotesDebug() {

        remotes = new ArrayList<>();

        Yaml yaml = new Yaml();
        RemoteConf conf206 = null;
        try {
            conf206 = yaml.loadAs(new FileReader(Constants.remoteConfDir + "206.yml"), RemoteConf.class);
            remotes.add(conf206);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    /**
     * 正式的查询
     */
    private static void initRemotes() {
        AuthorConf.notDebug();
        remotes = new ArrayList<>();
        Yaml yaml = new Yaml();
        try {
            RemoteConf qq = yaml.loadAs(new FileReader(Constants.remoteConfDir + "qq.yml"), RemoteConf.class);
            RemoteConf yy = yaml.loadAs(new FileReader(Constants.remoteConfDir + "yy.yml"), RemoteConf.class);
            RemoteConf qk = yaml.loadAs(new FileReader(Constants.remoteConfDir + "qk.yml"), RemoteConf.class);
            RemoteConf wan360 = yaml.loadAs(new FileReader(Constants.remoteConfDir + "360.yml"), RemoteConf.class);
            BufferedReader reader = new BufferedReader(new InputStreamReader(StartApp.class.getResourceAsStream("/remotes.txt")));
            String remotesStr = null;
            remotesStr = reader.readLine();
            if (remotesStr != null) {
                if (remotesStr.indexOf(qq.getRemoteType().getRemoteName()) != -1) {
                    remotes.add(qq);
                }
                if (remotesStr.indexOf(yy.getRemoteType().getRemoteName()) != -1) {
                    remotes.add(yy);
                }
                if (remotesStr.indexOf(qk.getRemoteType().getRemoteName()) != -1) {
                    remotes.add(qk);
                }
                if (remotesStr.indexOf(wan360.getRemoteType().getRemoteName()) != -1) {
                    remotes.add(wan360);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    public static void main(String[] args) {
        initRemotes();
        remotes.forEach((conf) -> {
            RemoteConnect.connect(conf, (result) -> {
                String[] codes = null;
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(StartApp.class.getResourceAsStream("/analysis.txt")));
                    String codesStr = reader.readLine();
                    codes = codesStr.split(",");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                List<String> notEndAct = new ArrayList<>();
                for (String s : codes) {
                    DataAnalysis dataAnalysis = null;
                    Class<DataAnalysis> dataAnalysisClass = AnalysisClient.getClient().getDataAnalysis(s);
                    AnalysisWare analysisWare = dataAnalysisClass.getAnnotation(AnalysisWare.class);
                    if (analysisWare != null) {
                        RemoteType[] remotes = analysisWare.remote();
                        boolean flag = false;
                        for (RemoteType t : remotes) {
                            flag = flag || (t == conf.getRemoteType());
                            if (flag) break;
                        }
                        if (!flag) {
                            continue;
                        }

                    }

                    try {
                        Constructor c = dataAnalysisClass.getConstructor(RemoteConnectResult.class, String.class);
                        dataAnalysis = (DataAnalysis) c.newInstance(result, s);
                        if (dataAnalysis.isTimeEnd()) {
                            dataAnalysis.execute();
                        } else {
                            notEndAct.add(s);
                        }
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } finally {

                    }
                }
                if (notEndAct.size() > 0) {
                    System.err.println(Arrays.deepToString(notEndAct.toArray()));
                }
            });


        });

    }
}
